package com.wxjfkg.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.sql.DataSource;

import org.activiti.spring.SpringAsyncExecutor;
import org.activiti.spring.SpringCallerRunsRejectedJobsHandler;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.SpringRejectedJobsHandler;
import org.activiti.spring.boot.ActivitiProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.task.TaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.StringUtils;

import com.wxjfkg.activiti.WxjfProcessEngineConfiguration;

//@Configuration
//@AutoConfigureAfter(MybatisConfig.class)
public class ProcessEngineConfig {

	@Autowired
	private DataSource dataSource;

	@Autowired
	private PlatformTransactionManager transactionManager;

	@Autowired
	protected ActivitiProperties activitiProperties;

	@Autowired
	private ResourcePatternResolver resourceLoader;

	@Bean
	public SpringAsyncExecutor springAsyncExecutor(TaskExecutor taskExecutor) {
		return new SpringAsyncExecutor(taskExecutor,
				springRejectedJobsHandler());
	}

	@Bean
	public SpringRejectedJobsHandler springRejectedJobsHandler() {
		return new SpringCallerRunsRejectedJobsHandler();
	}

	@Bean
	public SpringProcessEngineConfiguration springProcessEngineBean()
			throws Exception {
		List<Resource> procDefResources = this
				.discoverProcessDefinitionResources(this.resourceLoader,
						this.activitiProperties
								.getProcessDefinitionLocationPrefix(),
						this.activitiProperties
								.getProcessDefinitionLocationSuffix(),
						this.activitiProperties.isCheckProcessDefinitions());

		WxjfProcessEngineConfiguration conf = new WxjfProcessEngineConfiguration();
		conf.setDeploymentResources(procDefResources.toArray(new Resource[procDefResources.size()]));
		conf.setDataSource(dataSource);
		conf.setTransactionManager(transactionManager);

		conf.setDeploymentName(defaultText(activitiProperties.getDeploymentName(),
				conf.getDeploymentName()));
		conf.setDatabaseSchema(defaultText(activitiProperties.getDatabaseSchema(),
				conf.getDatabaseSchema()));
		conf.setDatabaseSchemaUpdate(activitiProperties.getDatabaseSchemaUpdate());
		conf.setJobExecutorActivate(activitiProperties.isJobExecutorActivate());
		conf.setAsyncExecutorEnabled(activitiProperties.isAsyncExecutorEnabled());
		conf.setAsyncExecutorActivate(activitiProperties.isAsyncExecutorActivate());
		return conf;
	}

	private List<Resource> discoverProcessDefinitionResources(
			ResourcePatternResolver applicationContext, String prefix,
			String suffix, boolean checkPDs) throws IOException {
		String path = prefix + suffix;
		if (checkPDs) {
			if (!applicationContext.getResource(prefix).exists()) {
				return new ArrayList<Resource>();
			}

			return Arrays.asList(applicationContext.getResources(path));
		}
		return new ArrayList<Resource>();
	}

	private String defaultText(String deploymentName, String defaultValue) {
		if (StringUtils.hasText(deploymentName)) {
			return deploymentName;
		}
		return defaultValue;
	}

}
